package cc.web;

import cc.entity.User;
import cc.entity.UserPassowrdEdit;
import cc.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * TODO
 *
 * @author My
 * @date 2021/10/11 11:00
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    IUserService userService;

    @GetMapping("/get")
    public String greeting() {
        return "cc so ...hhh";
    }

    @GetMapping("/getById/{id}/{name}")
    public User getById(@PathVariable("id") Integer id,@PathVariable("name") String name) {
        User user = new User();
        user.setId(id);
        user.setName(name);
        return user;
    }


    @GetMapping("/getByIds")
    public User getByIds(@RequestParam("id") Integer id,@RequestParam("name") String name) {
        User user = new User();
        user.setId(id);
        user.setName(name);
        return user;
    }

    @PostMapping("/getByIdss")
    public User getByIdss(Integer id) {
        User user = new User();
        user.setId(id);
        user.setName("张三");
        return user;
    }

    @PostMapping("/add")
    public User add(@RequestBody User user) throws Exception {
        userService.save(user);
        return user;
    }

    @GetMapping("/getByIdFromDb/{id}")
    public User getByIdFromDb(@PathVariable("id") Integer id) throws Exception {
        User user = userService.getById(id);
        return user;
    }

    @GetMapping("/getByIdFromDb2")
    public User getByIdFromDb2(@RequestParam("id") Integer id) throws Exception {
        User user = userService.getById2(id);
        return user;
    }

    @PostMapping("/passwordEdit")
    public User passwordEdit(@RequestBody UserPassowrdEdit userPassowrdEdit){

        User user = userService.update(userPassowrdEdit);
        return user;
    }
}
